package anthology.daily_question._24_11;

import java.util.HashMap;
import java.util.Map;

//https://leetcode.cn/problems/design-neighbor-sum-service/description/?envType=daily-question&envId=2024-11-09
public class _3242设计相邻元素求和服务 {
    class NeighborSum {
        private int[][] grid;
        private Map<Integer, int[]> pos;
        private final int[][][] dirs = {
                {{-1, 0}, {1, 0}, {0, -1}, {0, 1}},
                {{-1, -1}, {-1, 1}, {1, -1}, {1, 1}}
        };

        public NeighborSum(int[][] grid) {
            this.grid = grid;
            this.pos = new HashMap<>();
            for(int i=0; i<grid.length; i++){
                for(int j=0; j<grid[0].length; j++){
                    pos.put(grid[i][j],new int[]{i,j});
                }
            }
        }
        public int adjacentSum(int value) {
            return getSum(value, 0);

        }
        public int diagonalSum(int value) {
            return getSum(value, 1);
        }
        public int getSum(int value, int idx) {
            int[] p = pos.get(value);
            int x = p[0], y = p[1];
            int sum = 0;
            for(int[] dirs : dirs[idx]){
                int nx = x + dirs[0];
                int ny = y + dirs[1];
                if (nx >= 0 && nx < grid.length && ny >= 0 && ny < grid[0].length)
                    sum += grid[nx][ny];
            }
            return sum;
        }
    }

/**
 * Your NeighborSum object will be instantiated and called as such:
 * NeighborSum obj = new NeighborSum(grid);
 * int param_1 = obj.adjacentSum(value);
 * int param_2 = obj.diagonalSum(value);
 */
}
